package com.irdstudio.biz.drools.common;

import java.io.Serializable;

/**
 * 征信还款状态 出现逾期超N天
 */
public class RepayStatusContaintAccumulate extends RepayStatusBaseAccumulate {

    public void accumulate(Serializable context, Object value) {
        SumData data = (SumData) context;
        Object[] args = (Object[]) value;
        String repayStatus = (String) args[0];
        String months = (String) args[1];
        int month = Integer.valueOf(months);
        int mnNum = month;
        if (args.length >= 3) {
            mnNum = Integer.valueOf((String) args[2]);
        }
        String operator = ">";
        if (args.length >= 4) {
            operator = args[3] == null ? operator : (String) args[3];
        }
        int count = 0;
        if (repayStatus == null) {
            return;
        }

        log.debug("RepayStatusContaintAccumulate repayStatus {}, month {}, mnNum {}, operator {} ", repayStatus, month, mnNum, operator);
        String[] stats = splitRepayStatus(repayStatus);
        int len = stats.length;
        month = len - month;
        // 从后往前
        for (int i = len - 1; i >= month && i >= 0; i--) {
            if (stats[i].matches("\\d{1}")) {
                switch (operator) {
                    case ">":
                        if (Integer.valueOf(stats[i]).intValue() > mnNum) {
                            count++;
                        }
                        break;
                    case "==":
                        if (Integer.valueOf(stats[i]).intValue() == mnNum) {
                            count++;
                        }
                        break;
                    case "<":
                        if (Integer.valueOf(stats[i]).intValue() < mnNum) {
                            count++;
                        }
                        break;
                    case ">=":
                        if (Integer.valueOf(stats[i]).intValue() >= mnNum) {
                            count++;
                        }
                        break;
                    case "<=":
                        if (Integer.valueOf(stats[i]).intValue() <= mnNum) {
                            count++;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        data.total = Math.max(count, data.total);
        log.debug("RepayStatusContaintAccumulate accumulate {}, total {}", count, data.total);
    }
}
